/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.unittests;

import junit.framework.TestCase;

import org.aroundme.model.Search;
import org.aroundme.utility.SearchTokenizer;

public class SearchTokenizerTest extends TestCase {
	private SearchTokenizer tokenizer = null;
	
	protected void setUp() throws Exception {
		super.setUp();
		tokenizer = new SearchTokenizer();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		tokenizer = null;
	}

	public void testCreateSearchObject_AND() {
		String key1 = "restaurant";
		String key2 = "house";
		String key3 = "juri";
		String searchQuery = key1 + " and " + key2;
		
		Search search = tokenizer.createSearchObject(searchQuery);
		
		assertNotNull(search);
		assertEquals("length of AND keywords", 2, search.getKeywordsAND().size());
		
		boolean foundKey1 = false;
		boolean foundKey2 = false;
		
		for (String s : search.getKeywordsAND()) {
			if(s.equals(key1))
				foundKey1 = true;
			if(s.equals(key2))
				foundKey2 = true;
		}
		
		assertTrue(foundKey1);
		assertTrue(foundKey2);
		
		searchQuery = searchQuery + " " + key3;
		search = tokenizer.createSearchObject(searchQuery);
		
		assertNotNull(search);
		assertEquals("length of AND keywords", 3, search.getKeywordsAND().size());
		
		foundKey1 = false;
		foundKey2 = false;
		boolean foundKey3 = false;
		
		for (String s : search.getKeywordsAND()) {
			if(s.equals(key1))
				foundKey1 = true;
			if(s.equals(key2))
				foundKey2 = true;
			if(s.equals(key3))
				foundKey3 = true;
		}
		
		assertTrue(foundKey1);
		assertTrue(foundKey2);
		assertTrue(foundKey3);
	}
	
	public void testCreateSearchObject_OR(){
		String key1 = "restaurant";
		String key2 = "house";
		String key3 = "juri";
		String searchQuery = key1 + " or " + key2;
		
		Search search = tokenizer.createSearchObject(searchQuery);
		
		assertNotNull(search);
		assertEquals("length of OR keywords", 2, search.getKeywordsOR().size());
		
		boolean foundKey1 = false;
		boolean foundKey2 = false;
		
		for (String s : search.getKeywordsOR()) {
			if(s.equals(key1))
				foundKey1 = true;
			if(s.equals(key2))
				foundKey2 = true;
		}
		
		assertTrue(foundKey1);
		assertTrue(foundKey2);
		
		searchQuery = searchQuery + " or " + key3;
		search = tokenizer.createSearchObject(searchQuery);
		
		assertNotNull(search);
		assertEquals("length of OR keywords", 3, search.getKeywordsOR().size());
		
		foundKey1 = false;
		foundKey2 = false;
		boolean foundKey3 = false;
		
		for (String s : search.getKeywordsOR()) {
			if(s.equals(key1))
				foundKey1 = true;
			if(s.equals(key2))
				foundKey2 = true;
			if(s.equals(key3))
				foundKey3 = true;
		}
		
		assertTrue(foundKey1);
		assertTrue(foundKey2);
		assertTrue(foundKey3);
	}
	
	public void testCreateSearchObject_AND_OR(){
		String key1 = "juri";
		String key2 = "restaurant";
		String key3 = "computer";
		String key4 = "cat";
		String key5 = "house";
		
		String query = key1 + " " + key2 + " OR " + key3 + " or " + key4 + " or " + key5;

		Search search = tokenizer.createSearchObject(query);
		
		assertNotNull(search);
		assertEquals("Size of and components", 1, search.getKeywordsAND().size());
		assertEquals("Size of or components", 4, search.getKeywordsOR().size());
		
		boolean foundKey1 = false;
		boolean foundKey2 = false;
		boolean foundKey3 = false;
		boolean foundKey4 = false;
		boolean foundKey5 = false;
		
		for (String s : search.getKeywordsAND()) {
			if(s.equals(key1)){
				foundKey1 = true;
			}
		}
		
		for (String s : search.getKeywordsOR()) {
			if(s.equals(key2))
				foundKey2 = true;
			if(s.equals(key3))
				foundKey3 = true;
			if(s.equals(key4))
				foundKey4 = true;
			if(s.equals(key5))
				foundKey5 = true;
		}
		
		assertTrue(foundKey1 && foundKey2 && foundKey3 && foundKey4 && foundKey5);
	}
	
	public void testCreateSearchObject_AND_OR_2(){
		String key1 = "juri";
		String key2 = "restaurant";
		String key3 = "computer";
		String key4 = "cat";
		String key5 = "house";
		String key6 = "tom";
		
		String query = key1 + " and " + key6 + " " + key2 + " OR " + key3 + " or " + key4 + " or " + key5;

		Search search = tokenizer.createSearchObject(query);
		
		assertNotNull(search);
		assertEquals("Size of and components", 2, search.getKeywordsAND().size());
		assertEquals("Size of or components", 4, search.getKeywordsOR().size());
		
		boolean foundKey1 = false;
		boolean foundKey2 = false;
		boolean foundKey3 = false;
		boolean foundKey4 = false;
		boolean foundKey5 = false;
		boolean foundKey6 = false;
		
		for (String s : search.getKeywordsAND()) {
			if(s.equals(key1)){
				foundKey1 = true;
			}
			if(s.equals(key6))
				foundKey6 = true;
		}
		
		for (String s : search.getKeywordsOR()) {
			if(s.equals(key2))
				foundKey2 = true;
			if(s.equals(key3))
				foundKey3 = true;
			if(s.equals(key4))
				foundKey4 = true;
			if(s.equals(key5))
				foundKey5 = true;
		}
		
		assertTrue(foundKey1 && foundKey2 && foundKey3 && foundKey4 && foundKey5 && foundKey6);
	}

}
